{{- $pages := first 3 (where $.Site.Pages "Params.carousel" true) }}
{{- if and .IsHome $pages (eq .Paginator.PageNumber 1) }}
<div class="container p-0 mb-4">
    <div class="row p-0 m-0">
        <div id="carouselHome" class="carousel slide" data-bs-ride="carousel">
            <div class="carousel-indicators justify-content-sm-center justify-content-lg-start mb-0 mx-auto ps-0 ps-lg-4">
            {{- $i := 0 }}
            {{- range $pages }}
                <button type="button" data-bs-target="#carouselHome" data-bs-slide-to="{{ $i }}" aria-label="{{ .Title }}"
                {{- if eq $i 0 }}class="rounded-circle active" aria-current="true"{{ else }}class="rounded-circle"{{ end }}></button>
                {{- $i = add $i 1 }}
            {{- end }}
            </div>
            <div class="carousel-inner">
            {{- $i := 0 }}
            {{- range $pages }}
                <a class="carousel-item{{ if eq $i 0 }} active{{ end }}" href="{{ .RelPermalink }}">
                    {{- template "carousel-thumbnail" (dict "page" . "lazyLoading" (ne $i 0)) }}
                    <div class="carousel-caption ps-3 text-center text-lg-start start-0 end-0 mb-2">
                        <h5 class="mb-4 mb-lg-2">{{ .Title }}</h5>
                        <div class="d-none d-lg-flex mb-lg-4">{{ .Summary }}</div>
                    </div>
                </a>
                {{- $i = add $i 1 }}
            {{- end }}
            </div>
            <div class="carousel-controls position-absolute d-none d-lg-block">
                <a role="button" data-bs-target="#carouselHome" data-bs-slide="prev">
                    <span class="fas fa-chevron-circle-down fa-2x fa-fw text-secondary rounded rounded-circle" data-fa-transform="rotate-90" aria-hidden="true"></span>
                    <span class="visually-hidden">Previous</span>
                </a>
                <a role="button" data-bs-target="#carouselHome" data-bs-slide="next">
                    <span class="fas fa-chevron-circle-down fa-2x fa-fw text-secondary rounded rounded-circle" data-fa-transform="rotate-270" aria-hidden="true"></span>
                    <span class="visually-hidden">Next</span>
                </a>
            </div>
        </div>
    </div>
</div>
{{- end }}

{{- define "carousel-thumbnail" }}
{{- $smallImageURL := "/images/none.png" }}
{{- $mediumImageURL := "/images/none.png" }}
{{- $largeImageURL := "/images/none.png" }}
{{- $featured := false }}
{{- $lazyLoading := .lazyLoading }}
{{ if .page.Params.images }}
    {{ $smallImageURL = index .page.Params.images 0 | absURL }}
    {{ $mediumImageURL = $smallImageURL }}
    {{ $largeImageURL = $smallImageURL }}
{{- else }}
  {{- $featured := partialCached "functions/thumbnail" .page .page }}
  {{- with $featured }}
    {{- $ratio := div .Width $featured.Height }}
    {{- $smallImage := .Crop "320x180 smart" }}
    {{- $smallImageURL = $smallImage.RelPermalink }}
    {{- $mediumImage := .Crop "480x270 smart" }}
    {{- $mediumImageURL = $mediumImage.RelPermalink }}
    {{- $largeImage := .Crop "800x450 smart" }}
    {{- $largeImageURL = $largeImage.RelPermalink }}
  {{- end }}
{{- end }}
<picture>
    <source srcset="{{ $smallImageURL }}" media="(max-width: 576px)">
    <source srcset="{{ $mediumImageURL }}" media="(max-width: 991.98px)">
    <img class="d-block w-100" alt="{{ .Title }}" src="{{ $largeImageURL }}"{{ if $lazyLoading }} loading="lazy"{{ end }}
        {{ with $featured }} data-src="{{ .RelPermalink }}"{{ end }}/>
</picture>
{{- end -}}
